<?php
header("Content-type:text/json;charset=utf-8");


$t = new tt_i();

$t->ttA();

class tt_i{



    function do_curl($url, $params = array(), $upload = false, $type = 'POST')
    {
        $token = file_get_contents("token.txt");

        $method = strtoupper($type);
        if ($method == 'GET') {
            $url = "{$url}?" . http_build_query($params);
        }
        $ch = curl_init();
        //curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        //输出主机头
        if ($url=="https://capi.qumei.com/api/home/call"){
            //定义请求类型
            $username="webuser";
            $password="Qumei1993";
            $timestamp=time();

            $sign = md5($username.",".$password.",".$timestamp);

            $headers = array(
                "Content-Type: application/json",
                "Authorization: Bearer $token",
                "username: $username",
                "password: $password",
                "timestamp: $timestamp",
                "sign: $sign"
            );


            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        // 是否检测服务器的证书是否由正规浏览器认证过的授权CA颁发的
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // 是否检测服务器的域名与证书上的是否一致
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

        if ($method == 'POST') {
            if ($upload) {        //设置上传文件
                $file = new \CURLFile($upload['tmp_name'], $upload['type'], $upload['name']);
                $params[$upload['get_name']] = $file;
            }
            
            @curl_setopt($ch, CURLOPT_POST, 1);
            @curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        }
        $result = curl_exec($ch);
        curl_close($ch);
        if ($result === null || $result === false) {
            $this->error('操作出错！');
        } else {
            return $result;
        }
    }

     //获取token
    function gettoken()
    {

        $data = array();
        $data['grant_type']="password";
        $data['username']="webuser";
        $data['password']="Qumei1993";
        $out = self::do_curl("https://capi.qumei.com/token",$data,"","POST");
        $out = json_decode($out,true);
        file_put_contents("token.txt",$out["access_token"]);
        //var_dump($out["access_token"]);
    }

    //提交主体代码 

    function ttA()
    {
       

       if(self::selectYxTellBol($_POST['phone'])){


 
            $data["name"]= $_POST['name'];
            $data["phone"]= $_POST['phone'];
            $phone= $_POST['phone'];
            $renwuName=$_POST['renwuName'];

            // var_dump($renwuName);
            if(preg_match_all("/-sxd/U", $renwuName)>0){

                $data["ChannelID"]= '1290104747071770624'; //新时代派客渠道

            }elseif (preg_match_all("/-yj/U", $renwuName)>0) {



                if(self::selectyjTell($_POST['phone'])){


                    echo self:: upYjTellDB($_POST['phone']);

                    exit;

                }else{

                    $json = json_encode(array(
                    "code"=>0,
                    "msg"=>'已经提交',
                    "data"=>false ),
                    JSON_UNESCAPED_UNICODE);

                    echo $json;

                    exit;
                } 

            }else{

                $data["ChannelID"]= '1275707913033355264'; //朋友圈渠道
            };

            $data["dizhi"]= $_POST['dizhi'];
            $data["file_all"]= $_POST['file_all'];
            $data["list"]=$_POST['list'] ;
            $data = json_encode($data,JSON_UNESCAPED_UNICODE);
            $pp["Service"] = "com.QM.CallCenter.CommonChannelAPIV2";
            $pp["Param"] = $data; // addslashes($data)
            $pp = json_encode($pp,JSON_UNESCAPED_UNICODE);
            
            $out =self::do_curl("https://capi.qumei.com/api/home/call",$pp,false,"POST");
            $out = json_decode($out,true);


            if ($out['StatusCode']==200){

            
                echo self::selectYxTellDB($phone);


            }else{

                 $json = json_encode(array(
                    "code"=>0,
                    "msg"=>'提交失败:'.$out['StatusCode'].'错误信息：'.$out['ErrorMsg'],
                    "data"=>false ),
                    JSON_UNESCAPED_UNICODE);

                 

                echo $json;
            }   

        }else{



            $json = json_encode(array(
                    "code"=>0,
                    "msg"=>'已经提交',
                    "data"=>false ),
                    JSON_UNESCAPED_UNICODE);

            echo $json;


        };

       
    }

    function selectYxTellBol($tell){  //查询每日电话是否提交


        $qure="select submit from  everydaytell  WHERE tell='".$tell."'";
        $host='localhost';
        $user='root';
        $password='root';
        $database='weizandata';
        $link=mysqli_connect($host, $user, $password, $database);
        $link->query("set names utf8");
        $result=mysqli_query($link, $qure);
        $arr=mysqli_fetch_all($result, 1);
        mysqli_close($link);

        if($arr[0]['submit']!==null){

            return 0;

        }else{


            return 1;
        }

    }

    function selectyjTell($tell){  //查询优居电话是否提交


        $qure="select link_url from  youjutell  WHERE link_url='".$tell."'";
        $host='localhost';
        $user='root';
        $password='root';
        $database='weizandata';
        $link=mysqli_connect($host, $user, $password, $database);
        $link->query("set names utf8");
        $result=mysqli_query($link, $qure);
        $arr=mysqli_fetch_all($result, 1);
        mysqli_close($link);

        if(strlen($arr[0]['link_url']) == 11){

            return 0;

        }else{


            return 1;
        }

    }

    function selectYxTellDB($tell){   //更新数据库提交时间

       
        $qure="UPDATE everydaytell SET submit='".date('Y-m-d H:i:s')."',pingTai='".$_POST['pingTai']."' WHERE tell='".$tell."'";
        //echo $qure;
        $host='localhost';
        $user='root';
        $password='root';
        $database='weizandata';
        $link=mysqli_connect($host, $user, $password, $database);
        $link->query("set names utf8");
        $result=mysqli_query($link, $qure);
        $json = json_encode(array(
                "code"=>0,
                "msg"=>'提交成功',
                "data"=>$result ),
                JSON_UNESCAPED_UNICODE);
        
        
        mysqli_close($link);

        return $json;

    }

    function upYjTellDB($tell){   //更新优居数据库提交时间

       
        $qure="INSERT INTO youjuTell (link_url,title) VALUE('{$tell}','客户')";
        echo $qure;
        $host='localhost';
        $user='root';
        $password='root';
        $database='weizandata';
        $link=mysqli_connect($host, $user, $password, $database);
        $link->query("set names utf8");
        $result=mysqli_query($link, $qure);
        $json = json_encode(array(
                "code"=>0,
                "msg"=>'提交成功',
                "data"=>$result ),
                JSON_UNESCAPED_UNICODE);
        
        
        mysqli_close($link);

        return $json;

    }

}










